/**
 * 平面生成 - L5
 * @author lonphy
 * @version 0.1
 */
(function(L5) {
	"use strict";
	const X_BANDS = 30;
	const Y_BANDS = 30;
	class Plain {
		/**
		 * 平面构造
		 * @param width {Number} 平面宽度
		 * @param height {Number} 平面长度
		 */
		constructor(width, height, xb, yb) {
			this.width = width || 10;
			this.height = height || 10;

			this.vertices = [];
			this.indices = [];
			this.materialLibrary = {};
			this.maters = [];

			this.xb = xb || X_BANDS;
			this.yb = yb || Y_BANDS;

			this.create();
		}

		/**
		 * 构造平面
		 */
		create() {
			var vertex = this.vertices,
				indices = this.indices,
				i, j, first, second,
				xb = this.xb,
				yb = this.yb,
				startX = -this.width/ 2,
				startY = -this.height/ 2,
				dx = this.width/xb,
				dy = this.height/yb;

			for (i = 0; i <= xb; ++i) {
				var a = startX + i * dx;
				for (j = 0; j <= yb; j++) {
					var b = startY + j * dy;
					vertex.push(a, b, 0);					// position
					vertex.push(a, b, 0);					// normal
					vertex.push(1-i/xb, 1-j/yb, 0);
				}
			}
			for (i = 0; i < xb; ++i) {
				for (j = 0; j < yb; ++j) {
					first = (i * (yb + 1)) + j;
					second = first + yb + 1;
					indices.push(first, second, first + 1, second, second + 1, first + 1);
				}
			}
		}

		setMaterial(name, mater) {
			this.materialLibrary[name] = mater;
			this.maters.push(name);
		}

		getMesh() {
			return {
				vertices: this.vertices.slice(),
				indices: this.indices.slice(),
				materialLibrary: this.materialLibrary,
				maters: this.maters.slice(),
				hasIndex: true
			};
		}
	}

	L5.Geometry.Plain = Plain;
})(window.L5);